elgg.provide('elgg.tinymce_jquery');

/**
 * Toggles the tinymce_jquery editor
 *
 * @param {Object} event
 * @return void
 */
elgg.tinymce_jquery.toggleEditor = function(event) {
	event.preventDefault();
	
	var target = $(this).attr('href');
	var id = $(target).attr('id');
	if (!tinyMCE.get(id)) {
		tinyMCE.execCommand('mceAddControl', false, id);
		$(this).html(elgg.echo('tinymce:remove'));
	} else {
		tinyMCE.execCommand('mceRemoveControl', false, id);
		$(this).html(elgg.echo('tinymce:add'));
	}
}

/**
 * TinyMCE initialization script
 *
 * You can find configuration information here:
 * http://tinymce.moxiecode.com/wiki.php/Configuration
 */
elgg.tinymce_jquery.init = function() {

	$('.tinymce-toggle-editor').live('click', elgg.tinymce_jquery.toggleEditor);

	$('.elgg-input-longtext').parents('form').submit(function() {
		tinyMCE.triggerSave();
	});
/* вывод версии редактора взависимости от пользователя и места(коментарий или нет) */
<?php
if (elgg_is_admin_logged_in()) {
	$plugins = 'autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,youtube,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,hljs';
	$theme_advanced_buttons1 = 'save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect,|';
	$theme_advanced_buttons2 = 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,hljs,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor';
	$theme_advanced_buttons3 = 'tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,youtube,advhr,|,print,|,ltr,rtl,|,fullscreen';
	$theme_advanced_buttons4 = 'insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage';
} else {
	/*$plugins = 'lists,spellchecker,autosave,fullscreen,paste,media,hljs';
	$theme_advanced_buttons1 = 'bold,italic,underline,separator,strikethrough,bullist,numlist,undo,redo,hljs,link,unlink,image,blockquote,code,pastetext,pasteword,more,fullscreen,media';
	$theme_advanced_buttons2 = '';
	$theme_advanced_buttons3 = '';
	$theme_advanced_buttons4 = '';*/
	$plugins = 'autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,hljs';
	$theme_advanced_buttons1 = 'save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect,|';
	$theme_advanced_buttons2 = 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,hljs,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor';
	$theme_advanced_buttons3 = 'tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen';
	$theme_advanced_buttons4 = 'insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage';
}
?>
	tinyMCE.init({
		// Location of TinyMCE script
		//script_url : elgg.config.wwwroot + 'mod/tinymce/vendor/tinymce_jquery/jscripts/tiny_mce/tiny_mce.js',
			
		// General options
		mode : "specific_textareas",
		language : "<?php echo tinymce_get_site_language(); ?>",
		editor_selector : "elgg-input-longtext",
		theme : "advanced",
		plugins : "<?php echo $plugins ?>",
		relative_urls : false,
		remove_script_host : false,
		//remove_linebreaks : false,
		//forced_root_block : true,
		//force_p_newlines : true,
		//force_br_newlines : false,  //не использывать
		document_base_url : elgg.config.wwwroot,
		// Theme options
		theme_advanced_buttons1 : "<?php echo $theme_advanced_buttons1 ?>",
		theme_advanced_buttons2 : "<?php echo $theme_advanced_buttons2 ?>",
		theme_advanced_buttons3 : "<?php echo $theme_advanced_buttons3 ?>",
		theme_advanced_buttons4 : "<?php echo $theme_advanced_buttons4 ?>",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		theme_advanced_path : true,
		width : "100%",
		extended_valid_elements : "audio[src|width|height|autoplay|controls|loop|preload], video[src|width|height|audio|autoplay|controls|loop|poster|preload], iframe[src|width|height|name|align|frameborder|framespacing|frameborder|title], object[width|height|param|embed],param[name|value], embed[width|height|name|flashvars|src|bgcolor|align|play|loop|quality|allowscriptaccess|type|pluginspage], a[name|href|target|title|onclick|rel|class], img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name|style], hr[class|width|size|noshade], font[face|size|color|style], span[class|align|style], pre[name|class]",
		//media_strict: true,
		setup : function(ed) {
			
			//show the number of words
			ed.onLoadContent.add(function(ed, o) {
				var strip = (tinyMCE.activeEditor.getContent()).replace(/(&lt;([^&gt;]+)&gt;)/ig,"");
				var text = elgg.echo('tinymce:word_count') + strip.split(' ').length + ' ';
				tinymce.DOM.setHTML(tinymce.DOM.get(tinyMCE.activeEditor.id + '_path_row'), text);
			});

			ed.onKeyUp.add(function(ed, e) {
				var strip = (tinyMCE.activeEditor.getContent()).replace(/(&lt;([^&gt;]+)&gt;)/ig,"");
				var text = elgg.echo('tinymce:word_count') + strip.split(' ').length + ' ';
				tinymce.DOM.setHTML(tinymce.DOM.get(tinyMCE.activeEditor.id + '_path_row'), text);
			});
		},
		content_css: elgg.config.wwwroot + 'mod/tinymce/css/elgg_tinymce.css'
	});

	// work around for IE/TinyMCE bug where TinyMCE loses insert carot
	if ($.browser.msie) {
		$(".embed-control").live('hover', function() {
			var classes = $(this).attr('class');
			var embedClass = classes.split(/[, ]+/).pop();
			var textAreaId = embedClass.substr(embedClass.indexOf('embed-control-') + "embed-control-".length);

			if (window.tinyMCE) {
				var editor = window.tinyMCE.get(textAreaId);
				if (elgg.tinymce_jquery.bookmark == null) {
					elgg.tinymce_jquery.bookmark = editor.selection.getBookmark(2);
				}
			}
		});
	}
}

elgg.register_hook_handler('init', 'system', elgg.tinymce_jquery.init);